﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;

namespace Sapiens.at.SharePoint.Controls
{
    /// <summary>
    /// A derived save button throwing events before and after updating the item
    /// </summary>
    public class SaveButtonEx:SaveButton
    {
        /// <summary>
        /// Occurs before the ListItem is updated
        /// </summary>
        public event EventHandler OnBeforeUpdating;
        /// <summary>
        /// Occurs after the ListItem has been updated and the page ist valid
        /// </summary>
        public event EventHandler OnValidSave;
        /// <summary>
        /// Occurs after the ListItem has been updated and the page is invalid
        /// </summary>
        public event EventHandler OnInValidSave;
        /// <summary>
        /// Gets or sets the client-side script
        /// </summary>
        public string OnClientClick { get; set; }
        private bool _allowRedirection = true;
        /// <summary>
        /// Allow the redirection to the Source after the <see cref="SPListItem"/> has been updated
        /// </summary>
        public bool AllowRedirection
        {
            get { return _allowRedirection; }
            set { _allowRedirection = value; }
        }
        /// <summary>
        /// Registers an event handler on the button.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (Button != null) Button.OnClientClick += OnClientClick;
        }
        /// <summary>
        /// Validates the page and redirects to either <see cref="OnValidSave"/> or <see cref="OnInValidSave"/>.
        /// </summary>
        /// <returns></returns>
        protected override bool SaveItem()
        {
            bool saved = false;
            if (OnBeforeUpdating != null) OnBeforeUpdating(this, new EventArgs());
            Page.Validate();
            if (Page.IsValid)
            {
                saved = base.SaveItem();
                if (OnValidSave != null) OnValidSave(this, new EventArgs());
            }
            else if (OnInValidSave != null) OnInValidSave(this, new EventArgs());
            return saved && _allowRedirection;
        }
        internal Button _button;
        internal Button Button
        {
            get
            {
                if (this._button == null)
                    _button = (Button)this.TemplateContainer.FindControl("diidIOSaveItem");
                return _button;
            }
        }
    }
}
